<?php
namespace app\index\controller;
class Api extends Common{



    
    /**
     * 定时任务到期时加入执行队列
     * @param  integer $task_id 任务ID
     * @param  integer $time    请求接口时间
     * @param  string  $token   加密串
     * @return void
     */
    function pushTask($task_id=0,$time=0,$token=null){
        $str=$task_id.$time;
        $check=$this->checkToken($token,$str);
        if($check){
            $rs=model('tasking')->runTask($task_id,true);
            return json_encode($rs,JSON_UNESCAPED_UNICODE);
        }
        return "任务ID：$task_id 加入队列失败";
    }

    function getSoftBodyId(){
        $device_id=input('device_id/d',0);
        $softids=input('softids/a',[]);
        $software=model('software')->readAll(['device_id'=>$device_id,'id'=>['id','in',$softids]],0,['field'=>'body_id']);
        $s=[];
        foreach ($software as $v) {
            $s[]=$v['body_id'];
        }
        return json_encode($s,JSON_UNESCAPED_UNICODE);
    }
    // 验证来源于socket服务器的接口请求token
    private function checkToken($token=null,$str=null){
        if(is_null($token)||is_null($str)){
            return;
        }
        $_token=md5($str.config('setting.socket_code'));
        if($_token===$token){
            return true;
        }
    }

    #================关于城市信息控件的数据接口======================================#
    /**
     * 坐标转地址 （腾讯地图）
     * @param  [type] $lat [description]
     * @param  [type] $lng [description]
     * @return [type]      [description]
     */
    function qqLocation($lat,$lng){
        $url="http://apis.map.qq.com/ws/geocoder/v1/?location={$lat},{$lng}&key=".config('setting.qqmap_ak');
        return _curl($url,false);
    }
    /**
     * 地址转坐标 （腾讯地图）
     * @param  string $address 
     * @return [type]      
     */
    function qqAddress($address){
        $url="http://apis.map.qq.com/ws/geocoder/v1/?address={$address}&key=".config('setting.qqmap_ak');
        return _curl($url,false);
    }
    /**
     * 切换城市
     * @return [type] [description]
     */
    function citys(){
        $province=db('province')->select()->toArray();
        $citys=db('citys')->select()->toArray();
        foreach ($province as $k => $v) {
            foreach ($citys as $value) {
                if($v['id']===$value['provinceid']){
                    if(isset($province[$k]['children'])){
                        array_push($province[$k]['children'],$value);
                    }else{
                        $province[$k]['children']=[$value];
                    }
                }        
            }
        }
        return json('',0,$province);
    }

    /**
     * 获取区域分类
     * @return [type] [description]
     */
    function classify(){
        $list=model('classify')->readAll();
        return json('',0,$list);
    }
    /**
     * 获取城市
     * @return [type] [description]
     */
    function city(){
        $prefix=input('prefix/a',[]);
        if(!empty($prefix)){
            $data=model('city')->readAll(['prefix'=>['prefix','in',$prefix]]);
            $list=[];
            foreach ($data as $key => $v) {
                $list[$v['prefix']][]=$v;
            }
            return json('',0,$list);
        }
    }
    /**
     * 获取区域
     * @return [type] [description]
     */
    function area($city_id=0,$classify_id=1){
        if($classify_id<=0){
            $classify_id=1;
        }
        $ids=model('area_classify')->readAll(['city_id'=>$city_id,'classify_id'=>$classify_id],0,['field'=>'area_id']);
        foreach ($ids as $k => $v) {
            $ids[$k]=$v['area_id'];
        }
        $list=model('area')->readAll(['id'=>['id','in',$ids]]);
        return json('',0,$list);
    }
}
